package com.xc.qq;

import org.json.JSONObject;

import android.app.Activity;
import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

import com.tencent.tauth.IUiListener;
import com.tencent.tauth.Tencent;
import com.tencent.tauth.UiError;

public class MainActivity extends Activity {

	private Tencent mTencent;
	private TextView tv;
	private Button btn;
	private Handler  mHandler;
	private Dialog mProgressDialog;
	private static final String SCOPE = "get_user_info,get_simple_userinfo,get_user_profile,get_app_friends,"
            + "add_share,add_topic,list_album,upload_pic,add_album,set_user_face,get_vip_info,get_vip_rich_info,get_intimate_friends_weibo,match_nick_tips_weibo";

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		mTencent = Tencent.createInstance("100465667", this.getApplicationContext());
		tv = (TextView)findViewById(R.id.tv);
		btn=  (Button)findViewById(R.id.btn);
		btn.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				onClickLogin();
			}
		});
	}
	 private void onClickLogin() {
		 if (!mTencent.isSessionValid()) {
	            IUiListener listener = new BaseUiListener() {
	                @Override
	                protected void doComplete(JSONObject values) {
	                }
	            };
	            mTencent.login(this, SCOPE, listener);
	        } else {
	            mTencent.logout(this);
	        }
	    }
	 private class BaseUiListener implements IUiListener {

	        @Override
	        public void onComplete(JSONObject response) {
	            tv.setText("onComplete:");
	            tv.setText(response.toString());
	            doComplete(response);
	        }

	        protected void doComplete(JSONObject values) {

	        }

	        @Override
	        public void onError(UiError e) {
	            showResult("onError:", "code:" + e.errorCode + ", msg:"
	                    + e.errorMessage + ", detail:" + e.errorDetail);
	        }

	        @Override
	        public void onCancel() {
	            showResult("onCancel", "");
	        }
	    }
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}
	@Override
	 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
	       mTencent.onActivityResult(requestCode, resultCode, data) ;
	 }
	 private void showResult(final String base, final String msg) {
	     mHandler.post(new Runnable() {

	            @Override
	            public void run() {
	                if (mProgressDialog.isShowing())
	                    mProgressDialog.dismiss();
	                tv.setText(base);
	                tv.setText(base+"/////////"+msg);
	            }
	        });
	    }
}
